package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppMenuDTO;
import com.info33.platform.business.app.entity.AppMenu;
import com.info33.platform.business.app.service.AppMenuService;
import com.info33.platform.business.app.vo.AppMenuVO;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppMenu")
@Api(tags = "app - 菜单")
public class AppMenuController extends BaseController<AppMenuService, AppMenu> {

    private final AppMenuService appMenuService;

    @Autowired
    public AppMenuController(AppMenuService appMenuService) {
        super(appMenuService);
        this.appMenuService = appMenuService;
    }

    @GetMapping(value = "/pc/tree")
    @ApiOperation(value = "pc - 菜单树", notes = "所有基础菜单，无分页")
    public Result<List<AppMenuVO>> pcTree() {
        return Result.ok(appMenuService.pcTree());
    }

    @PostMapping(value = "/pc/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 菜单分页", notes = "入参说明：<br/>" +
            "pid = 1：顶级菜单<br/>" +
            "pid = ?：二级菜单（menuVO.id 取值）")
    public Result<IPage<AppMenu>> pcPage(
            @PathVariable int pageNum,
            @PathVariable int pageSize,
            @RequestBody AppMenuDTO dto) {
        return Result.ok(appMenuService.page(new Page<>(pageNum, pageSize), new QueryWrapper<AppMenu>()
                .eq("ABS(pid)", dto.getPid())));
    }

    @PostMapping(value = "/pc/add")
    @ApiOperation(value = "pc - 新增菜单", notes = "仅超级管理员可以新增")
    public Result<Object> pcAdd(
            @RequestBody AppMenu appMenu) {
        if (Boolean.FALSE.equals(SysUtil.isSysAdmin())) {
            return Result.error("操作失败，原因：权限不足");
        }
        return appMenuService.pcAdd(appMenu);
    }

    @DeleteMapping(value = "/pc/delete/{mid}")
    @ApiOperation(value = "pc - 删除", notes = "mid：app_menu id")
    public Result<Object> pcDelete(
            @PathVariable(value = "mid") Long mid) {
        return appMenuService.pcDelete(mid);
    }

}